export function throttle(fn, duration = 2000) {
    let timer = null;
    let lastTime = Date.now();
    return function (...rest) {
        let now = Date.now();
        if (now - lastTime >= duration) {
            clearTimeout(timer);
            timer = null;
            lastTime = now;
            return fn.apply(this, rest);
        } else if (!timer) {
            timer = setTimeout(() => {
                return fn.apply(this, rest);
            }, duration)
        }
    }
}

/**
 * 防抖
 */
export function debounce(fn, delay = 2000) {
    let timer = null;
    return function (...rest) {
        if (!timer) {
            fn.apply(this, rest);
        }
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.apply(this, rest);
        }, delay)
    }
}
